package com.iims.product;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.iims.common.annotation.Log;
import com.iims.common.constant.UserConstants;
import com.iims.common.core.controller.BaseController;
import com.iims.common.core.domain.AjaxResult;
import com.iims.common.core.page.TableDataInfo;
import com.iims.common.enums.BusinessType;
import com.iims.product.domain.Product;
import com.iims.product.service.IProductService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

/**
 * @author fanpei
 * @date 2021/6/11 14:33
 */
@Api(tags = "G-产品管理")
@RestController
@RequestMapping("/business/product")
public class ProductController extends BaseController {
    @Autowired
    private IProductService productService;

    /**
     * 获取产品列表
     */
    @ApiOperation("分页查询产品列表")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "当前记录起始索引(从1开始)", name = "pageNum", paramType = "query", required = true),
            @ApiImplicitParam(value = "每页显示记录数（>0）", name = "pageSize", paramType = "query", required = true),
            @ApiImplicitParam(value = "排序列", name = "orderByColumn", paramType = "query"),
            @ApiImplicitParam(value = "升降序方式：0升序 1降序", name = "isAsc", paramType = "query"),
            @ApiImplicitParam(value = "产品名称[模糊匹配]", name = "productName", paramType = "query"),
            @ApiImplicitParam(value = "产品类型Id[精确匹配]", name = "prodTypeId", paramType = "query"),
            @ApiImplicitParam(value = "产品负责人[模糊匹配]", name = "chargeMan", paramType = "query")
    })
    @PreAuthorize("@ss.hasPermi('business:product:list')")
    @GetMapping("/list")
    public TableDataInfo list(@ApiIgnore Product product) throws Exception {
        startPage();
        List<Product> list = productService.selectProductList(product);
        return getDataTable(list);
    }

    /**
     * 根据产品编号获取详细信息
     */
    @ApiOperation("根据产品编号获取详细信息")
    @ApiImplicitParam(name = "productId", value = "产品Id", paramType = "path", required = true)
    @PreAuthorize("@ss.hasPermi('business:product:query')")
    @GetMapping(value = "/{productId}")
    public AjaxResult getInfo(@PathVariable Long productId) {
        return AjaxResult.success(productService.selectOne(productId));
    }

    /**
     * 新增产品
     */
    @ApiOperation("新增产品")
    @ApiOperationSupport(ignoreParameters = {"productId", "prodTypeName"})
    @PreAuthorize("@ss.hasPermi('business:product:add')")
    @Log(title = "产品管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody Product product) {
        if (UserConstants.NOT_UNIQUE.equals(productService.checkProductNameUnique(product))) {
            return AjaxResult.error("新增产品'" + product.getProductName() + "'失败，产品名称已存在");
        }
        return toAjax(productService.insertProduct(product));
    }

    /**
     * 修改产品
     */
    @ApiOperation("修改产品")
    @ApiOperationSupport(ignoreParameters = {"prodTypeName"})
    @PreAuthorize("@ss.hasPermi('business:product:edit')")
    @Log(title = "产品管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody Product product) {
        if (UserConstants.NOT_UNIQUE.equals(productService.checkProductNameUnique(product))) {
            return AjaxResult.error("修改产品'" + product.getProductName() + "'失败，产品名称已存在");
        }
        return toAjax(productService.updateProduct(product));
    }

    /**
     * 删除产品
     */
    @ApiOperation("删除产品")
    @PreAuthorize("@ss.hasPermi('business:product:remove')")
    @Log(title = "产品管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{productId}")
    public AjaxResult remove(@ApiParam("产品Id") @PathVariable Long productId) {
        if (productService.checkProductUse(productId)) {
            return AjaxResult.error("删除产品失败，产品已使用！请先删除关联产品的合同！");
        }
        return toAjax(productService.deleteProductById(productId));
    }
}
